package com.it.schoolhomeos.service.impl;

import com.it.schoolhomeos.entity.Exam;
import com.it.schoolhomeos.repository.ExamRepository;
import com.it.schoolhomeos.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ExamService接口的实现类
 * 
 * 实现考试相关的业务逻辑
 */
@Service
@Transactional
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamRepository examRepository;
    
    /**
     * 获取所有考试
     * 
     * @return 考试列表
     */
    @Override
    public List<Exam> getAllExams() {
        return examRepository.findAll(Sort.by(Sort.Direction.DESC, "examDate"));
    }
    
    /**
     * 分页获取考试
     * 
     * @param page 页码
     * @param size 每页大小
     * @return 分页的考试列表
     */
    @Override
    public Page<Exam> getExams(int page, int size) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "examDate"));
        return examRepository.findAll(pageable);
    }
    
    /**
     * 根据ID获取考试
     * 
     * @param id 考试ID
     * @return 考试对象
     */
    @Override
    public Exam getExamById(Long id) {
        return examRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("考试不存在"));
    }
    
    /**
     * 根据名称查找考试
     * 
     * @param name 考试名称
     * @return 考试列表
     */
    @Override
    public List<Exam> findByName(String name) {
        return examRepository.findByName(name);
    }
    
    /**
     * 根据日期查找考试
     * 
     * @param date 考试日期
     * @return 考试列表
     */
    @Override
    public List<Exam> findByDate(LocalDate date) {
        return examRepository.findByExamDate(date);
    }
    
    /**
     * 根据日期范围查找考试
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 考试列表
     */
    @Override
    public List<Exam> findByDateRange(LocalDate startDate, LocalDate endDate) {
        return examRepository.findByExamDateBetween(startDate, endDate);
    }
    
    /**
     * 根据科目查找考试
     * 
     * @param subject 科目
     * @return 考试列表
     */
    @Override
    public List<Exam> findBySubject(String subject) {
        return examRepository.findBySubjectContaining(subject);
    }
    
    /**
     * 创建考试
     * 
     * @param exam 考试对象
     * @return 创建后的考试对象
     */
    @Override
    public Exam createExam(Exam exam) {
        if (exam.getId() != null) {
            throw new RuntimeException("新建考试不应该包含ID");
        }
        
        LocalDateTime now = LocalDateTime.now();
        exam.setCreateTime(now);
        exam.setUpdateTime(now);
        
        return examRepository.save(exam);
    }
    
    /**
     * 更新考试
     * 
     * @param exam 考试对象
     * @return 更新后的考试对象
     */
    @Override
    public Exam updateExam(Exam exam) {
        if (exam.getId() == null) {
            throw new RuntimeException("更新考试必须包含ID");
        }
        
        Exam existingExam = examRepository.findById(exam.getId())
                .orElseThrow(() -> new RuntimeException("考试不存在"));
        
        exam.setCreateTime(existingExam.getCreateTime());
        exam.setUpdateTime(LocalDateTime.now());
        
        return examRepository.save(exam);
    }
    
    /**
     * 删除考试
     * 
     * @param id 考试ID
     */
    @Override
    public void deleteExam(Long id) {
        if (!examRepository.existsById(id)) {
            throw new RuntimeException("考试不存在");
        }
        examRepository.deleteById(id);
    }
    
    /**
     * 获取考试的科目列表
     * 
     * @param examId 考试ID
     * @return 科目列表
     */
    @Override
    public List<String> getExamSubjects(Long examId) {
        Exam exam = examRepository.findById(examId)
                .orElseThrow(() -> new RuntimeException("考试不存在"));
        
        return exam.getSubjectList();
    }
} 